package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.gift.NoviceItemData;
import com.zhongxi.guoyuan.entity.gift.NoviceItemResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;
import com.zhongxi.guoyuan.view.user.gift.GiftDetailActivity;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/12/19.
 */

public class GiftAdapter extends BaseAdapter {

    private Context context;
    private List<NoviceItemData> list = new ArrayList<>();
    private NoviceItemData data;
    private JSONObject goods;
    private JSONObject item;
    private JSONObject market;
    private JSONObject jsonObject;


    public GiftAdapter(Context context, List<NoviceItemData> data) {
        this.context = context;
        this.list = data;
    }

    public JSONObject getGoods() {
        return goods;
    }

    public void setGoods(JSONObject goods) {
        this.goods = goods;
    }

    public JSONObject getItem() {
        return item;
    }

    public void setItem(JSONObject item) {
        this.item = item;
    }

    public JSONObject getMarket() {
        return market;
    }

    public void setMarket(JSONObject market) {
        this.market = market;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_user_gift,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        data = list.get(position);
        if (data!=null){

            if (null!=data.getName()&&!"".equals(data.getName())){
                holder.giftActivityName.setText(data.getName());
            }else {
                holder.giftActivityName.setText("");
            }

            if (data.getImages()!=null){
                Glide.with(context).load(Url.IMG_URL + data.getImages()).placeholder(R.mipmap.iv_eg).into(holder.giftProjectIcon);
            }
            if (null!=data.getAprice()&&!"".equals(data.getAprice())){
                holder.giftProjectSharePrice.setText("分享价："+data.getAprice()+"元");
            }else {
                holder.giftProjectSharePrice.setText("分享价：");
            }
            if (null!=data.getBackinnernum()&&!"".equals(data.getBackinnernum())&&data.getBackredbeanfcustomer()!=null&&!"".equals(data.getBackredbeanfcustomer())){
                holder.giftFriendgetMeidou.setText(data.getBackinnernum()+"美豆");
                holder.giftFriendgetHongdou.setText(data.getBackredbeanfcustomer()+"红豆");
            }else if (null!=data.getBackinnernum()&&!"".equals(data.getBackinnernum())){
                holder.giftFriendgetMeidou.setText(data.getBackinnernum()+"美豆");
                holder.giftFriendgetHongdou.setText("");
            }else if (data.getBackredbeanfcustomer()!=null&&!"".equals(data.getBackredbeanfcustomer())){
                holder.giftFriendgetMeidou.setText("");
                holder.giftFriendgetHongdou.setText(data.getBackredbeanfcustomer()+"红豆");
            }else {
                holder.giftFriendgetMeidou.setText("");
                holder.giftFriendgetHongdou.setText("");
            }
            if (null!=data.getBackredbeanfshare()&&!"".equals(data.getBackredbeanfshare())){
                holder.giftSharegetHongdou.setText(data.getBackredbeanfshare()+"红豆");
            }else {
                holder.giftSharegetHongdou.setText("");
            }
            if (null!=data.getEtime()&&!"".equals(data.getEtime())){
                holder.giftActivityEtime.setText(data.getEtime());
            }else {
                holder.giftActivityEtime.setText("");
            }
            //1 美容项目， 2 超市商品， 3 自定义商品

            String atype = data.getAtype();
            if(null!=atype&&!"".equals(atype)){
                if (atype.equals("1")){
                    jsonObject = item.optJSONObject(data.getRefid());
                }else if (atype.equals("2")){
                    jsonObject = market.optJSONObject(data.getRefid());
                }else if (atype.equals("3")){
                    jsonObject = goods.optJSONObject(data.getRefid());
                }
            }

            if(null!=jsonObject&&!"".equals(jsonObject)){
                String name = jsonObject.optString("name");
                String marketprice = jsonObject.optString("marketprice");
                if (null!=name&&!"".equals(name)){
                    holder.giftProjectName.setText(name);
                }else {
                    holder.giftProjectName.setText("");
                }

                if (null!=marketprice&&!"".equals(marketprice)){
                    holder.giftProjectMarketPrice.setText("市场价："+marketprice+"元");
                }else {
                    holder.giftProjectMarketPrice.setText("市场价：");
                }
            }

        }

        return convertView;

    }

    static class ViewHolder {
        @Bind(R.id.gift_activity_name)
        TextView giftActivityName;
        @Bind(R.id.gift_project_icon)
        ImageView giftProjectIcon;
        @Bind(R.id.gift_project_name)
        TextView giftProjectName;
        @Bind(R.id.gift_project_marketPrice)
        TextView giftProjectMarketPrice;
        @Bind(R.id.gift_project_sharePrice)
        TextView giftProjectSharePrice;
        @Bind(R.id.gift_friendget_meidou)
        TextView giftFriendgetMeidou;
        @Bind(R.id.gift_friendget_hongdou)
        TextView giftFriendgetHongdou;
        @Bind(R.id.gift_shareget_hongdou)
        TextView giftSharegetHongdou;
        @Bind(R.id.gift_item_project)
        LinearLayout giftItemProject;
        @Bind(R.id.gift_activity_etime)
        TextView giftActivityEtime;
        @Bind(R.id.gift_avtivity_share)
        RelativeLayout giftAvtivityShare;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
